//
//  ListViewController.m
//  BiDongComic
//
//  Created by 吴炳毅 on 16/3/22.
//  Copyright © 2016年 BestRiven. All rights reserved.
//

//http://api.kuaikanmanhua.com/v1/topic_lists/4?limit=20&offset=0    //4
//http://api.kuaikanmanhua.com/v1/topic_lists/1?limit=20&offset=0    //1
//http://api.kuaikanmanhua.com/v1/topic_lists/5?limit=20&offset=0    //5
//http://api.kuaikanmanhua.com/v1/topic_lists/3?limit=20&offset=0    //3
//http://api.kuaikanmanhua.com/v1/topic_lists/2?limit=20&offset=0    //2
//http://api.kuaikanmanhua.com/v1/topic_lists/others?limit=20&offset=0  //others

#define LISTURLSTR @"http://api.kuaikanmanhua.com/v1/topic_lists/%@?limit=20&offset=0"
#import "ListViewController.h"
#import "ListTableViewCell.h"
#import "AFNetworking/AFNetworking.h"
#import "ListModel.h"
#import <UIImageView+WebCache.h>
#import "ComicGridViewController.h"



@interface ListViewController ()<UITableViewDataSource,UITableViewDelegate> {
    NSMutableArray *arrayAllData;
}

@end

@implementation ListViewController



- (void)loadView {
    self.listView = [[ListView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.listView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setViews];
    [self setDelegate];
    [self setDatas];
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.tempView removeFromSuperview];
}


#pragma mark - 视图设置
- (void)setViews {
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ic_up_indicator.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction)];
    
    self.navigationItem.leftBarButtonItem = item;
    //判断跳转状态
    if (self.more_select == 1) {
        self.title = self.naVTitle;

    }
#warning - 添加view;
    //i
    if (self.more_select == 2) {
        self.tempView = [[UIView alloc]initWithFrame:CGRectMake(60, 0, kScreenWidth, 44)];
        self.tempView.backgroundColor = [UIColor whiteColor];
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(-60, 0, kScreenWidth, 44)];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.text = self.title;
        [self.tempView addSubview:lable];
        
        [self.navigationController.navigationBar addSubview:self.tempView];
        
    }
    
    

}

#pragma mark - 数据设置
- (void)setDatas {
    NSString *urlStr = [NSString stringWithFormat:LISTURLSTR,self.more_id];
    NSString *requestString = [[NSString alloc]init];
    if (self.sessionUrlStr.length != 0) {
        requestString = [NSString stringWithFormat:@"%@",self.sessionUrlStr];
        
    }else {
        requestString = urlStr;
    }
    
    
    //初始化数组
    arrayAllData = [NSMutableArray array];
    //
    //所有漫画数据列表
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
 
    [session GET:requestString parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        arrayAllData = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"data"][@"topics"]) {
                ListModel *model = [[ListModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [arrayAllData addObject:model];
        }
        //回到主线程 给ui赋值
        [self performSelectorOnMainThread:@selector(mainAction) withObject:nil waitUntilDone:YES];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"失败");
    }];
}


- (void)mainAction {
    self.HUD.hidden = YES;
    [self.listView.listTableView reloadData];
    
    
}





#pragma mark - tableView设置代理和注册
- (void)setDelegate {
    self.listView.listTableView.delegate = self;
    self.listView.listTableView.dataSource = self;
    [self.listView.listTableView registerNib:[UINib nibWithNibName:@"ListTableViewCell" bundle:nil] forCellReuseIdentifier:@"listCell_id"];
}


#pragma mark - tableViewDelegate 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return arrayAllData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listCell_id" forIndexPath:indexPath];
    if (arrayAllData.count != 0) {
        ListModel *model = arrayAllData[indexPath.row];
        //图片
        [cell.imV sd_setImageWithURL:[NSURL URLWithString:model.cover_image_url]];
        //标题
        cell.title.text = model.title;
        cell.title.font = [UIFont systemFontOfSize:18];
        cell.title.textColor = [UIColor lightGrayColor];
        //作者
        cell.editor.text = model.user[@"nickname"];
        cell.editor.font = [UIFont systemFontOfSize:14];
        cell.editor.textColor = [UIColor lightGrayColor];
        //点赞人数
        cell.likes_count.text = [NSString stringWithFormat:@"%ld万",model.likes_count/10000];
        cell.likes_count.font = [UIFont systemFontOfSize:13];
        cell.likes_count.textColor = [UIColor lightGrayColor];
        cell.comments_count.text = [NSString stringWithFormat:@"%ld",model.comments_count];
        cell.comments_count.font = [UIFont systemFontOfSize:13];
        cell.comments_count.textColor = [UIColor lightGrayColor];
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}


#pragma mark - itemAction
- (void)itemAction {
    [self.navigationController popViewControllerAnimated:YES];
 
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
